package com.itheima.health.controller;

import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.utils.resources.UploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@Api(tags = "套餐管理开发")
public class SetmealController {


    // springmvc  消息转化器   @ResponseBody  默认 全字段序列化

    @Reference
    private SetmealService setmealService;

//    @GetMapping("checkitem/findAll")
//    @ApiOperation(value = "查询功能",notes = "查询所有的检查项列表信息")
//    public Result findAll(HttpServletResponse response){
//
//        return new Result(checkItemService.list());
//    }
    @PostMapping("setmeal/findPage")
    @ApiOperation(value = "查询功能",notes = "根据检查组id查询对应检查项明细")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        return new Result(setmealService.findPage(queryPageBean));
    }

    @PostMapping("setmeal/upload")
    @ApiOperation(value = "分页查询功能",notes = "查询所有的检查组列表信息")
    public Result upload(@RequestParam("imgFile") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();//  获取上传文件真实名称
        String uuidfilename = UploadUtils.generateRandonFileName(originalFilename);// 基于真实文件扩展名 生成uuid名称
        //  文件上传oss
        AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(),uuidfilename);
        //  图片存储 集合 + 单独保存一个过期时间
        RedisUtil.addToSet(RedisConstant.ALL_SETMEAL_PIC_SET,uuidfilename);
        RedisUtil.set(RedisConstant.SINGLE_PIC+uuidfilename,uuidfilename,24, TimeUnit.HOURS);
        return new Result(uuidfilename);
    }
//
//    @DeleteMapping("checkitem/deleteCheckItemById/{id}")
//    @ApiOperation(value = "分页查询功能",notes = "查询所有的检查项列表信息")
//    public Result deleteCheckItemById(@PathVariable("id") int id){
//        CheckItem checkItem = new CheckItem();
//        checkItem.setId(id);
//        checkItem.setIs_delete(1);  //  update .... set  is_delete = 1 where id = ?
//         return  new Result(checkItemService.updateById(checkItem));
//    }

    @PostMapping("setmeal/add")
    @ApiOperation(value = "添加功能",notes = "添加套餐")
    public Result  add(@RequestBody SetmealDTO setmealDTO ){
        return  new Result(setmealService.saveUpdate(setmealDTO));

    }


    @GetMapping("setmeal/findCheckGroupInfoBySetmealId/{id}")
    public Result findCheckGroupInfoBySetmealId(@PathVariable("id") int id){
        return new Result(setmealService.findCheckGroupInfoBySetmealId(id));
    }
}





